package ar.utn.jess.test;

import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class SimulacionMensajes {

	public static String IP = "localhost:8080";
	public static String URL_CENTRAL = "http://" + IP + "/central/comunicacion";
	
//	public static String IP = "176.58.97.39:80";
//	public static String URL_CENTRAL = "http://" + IP + "/central/comunicacion";
	
	
	public static void main(String[] args) {
		
		SimulacionMensajes sm = new SimulacionMensajes();
		
		sm.login("ez", "123ee", "-34.6047139", "-58.4021210999");		
//		sm.login("Rudi", "Rudi", "-34.5861496", "-58.442429");
		
//		sm.login("ezequiel", "ezequiel", "-34.6047139", "-58.4021210999");		
//		sm.login("pruebas", "pruebas", "-34.5861496", "-58.442429");
		
//		sm.actualizarEstado(4, "-34.6047139", "-58.4021210999", "1", "2");
		
		
//		sm.actualizarEstado(1, "-34.6047139", "-58.4021210999", "1", "2"); // libre asignar
		
//		sm.actualizarEstado(1, "-34.6047139", "-58.4021210999", "0", "1");  // confirmar a bordo
		
//		sm.actualizarEstado(1, "-34.6047139", "-58.4021210999", "2", "1");  // logout
		

//		sm.actualizarEstado(2, "-34.5861496", "-58.442429", "1", "2");
		
//		sm.actualizarEstado(2, "-34.5861496", "-58.442429", "0", "1");
		
		
		// desocupado, sin asignacion
//		sm.actualizarEstado(1, "223.22", "377.1", "1", "0");
		
		// ocupado, solo actualizacion posicion
//		sm.actualizarEstado(1, "250.22", "390.51", "0", "1");
		
		// desocupado, finalizar viaje
//		sm.actualizarEstado(1, "276.11", "432.2", "1", "2");
		
		
//		sm.logout("ezequiel", "ezequiel");
//		sm.logout("ez", "123ee");
//		sm.logout("Rudi", "Rudi");
		
	}
	


	public InputStream login(String usr, String pass, String lat, String longi) {
        
		String urlParameters = "user=" + usr + "&pass=" + pass + "&lat=" + lat + "&long=" + longi;
		
		return callService(urlParameters);
	}
	
	public InputStream logout(String usr, String pass) {
        
		String urlParameters = "user=" + usr + "&pass=" + pass + "&logout=1";
		
		return callService(urlParameters);
	}
	
	public InputStream actualizarEstado(int idchofer, String lat, String longi, String est_chofer, String est_viaje) {
		
		String urlParameters = "idchofer=" + idchofer + "&lat=" + lat + "&long=" + longi + "&estado_chofer=" + est_chofer + "&estado_viaje=" + est_viaje;
		
		return callService(urlParameters);
	}
	
	
	private InputStream callService(String urlParameters) {
		
		InputStream in = null;

        try {
             URL url = new URL(URL_CENTRAL);
             URLConnection conn = url.openConnection();

             HttpURLConnection httpConn = (HttpURLConnection) conn;
             httpConn.setRequestMethod("POST");
             httpConn.setDoInput(true);
             httpConn.setDoOutput(true);
             httpConn.connect();

             DataOutputStream dataStream = new DataOutputStream(conn.getOutputStream());

                         
             dataStream.writeBytes(urlParameters);
             dataStream.flush();
             dataStream.close();

             int responseCode = httpConn.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK) {
                  in = httpConn.getInputStream();
             }
        } catch (Exception ex) {
             System.out.println("Error: Not not connect");
        }
        return in;
	}

}
